package com.zh.springcloud.controller;

import com.zh.springcloud.config.DirectRabbitConfig;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 测试发送rabbitMq 消息
 * @ClassName TestRabbisMQSendController
 * @date: 2021.08.20 10:26
 *
 */
@RestController
public class TestRabbisMQSendController {

	@Autowired
	private RabbitTemplate rabbitTemplate;


	@GetMapping("/direct/sendMsg")
	public String directSendMsg(){
		String msg = "hello World";
		rabbitTemplate.convertAndSend(DirectRabbitConfig.EXCHANGE_NAME,DirectRabbitConfig.ROUTING,msg);
		return "ok~";
	}

	@GetMapping("/sendMsg")
	public String sendMsg(@RequestParam("exchange") String exchange){
		String msg = "hello World";
		rabbitTemplate.convertAndSend(exchange,null,msg);
		return "ok~";
	}

	@GetMapping("/topic/sendMsg")
	public String topicSendMsg(){
		String msg = "hello World";
		System.out.println("topicSendMsg{}"+  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

		rabbitTemplate.convertAndSend("test_topic_exchange_name","test_topic_routing_name",msg);

		return "ok~";
	}

	@GetMapping("/topic/sendMsg2")
	public String topicSendMsg2(){
		String msg = "hello World";
		// 对每条消息设置过期时间
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.setDelay(1 * 60 * 1000);
//		messageProperties.setExpiration("20000"); // 时间单位为 毫秒
		Message message = new Message(msg.getBytes(),messageProperties);
		System.out.println("topicSendMsg2{}"+  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		rabbitTemplate.convertAndSend("test_topic_exchange_name1","test_topic_routing_name1",message);
		return "ok~";
	}
}
